<template>
  <ul class="app-header-nav">
    <li class="home"><RouterLink to="/">首页</RouterLink></li>
    <li v-for="(v, i) in headerObj" :key="i">
      <router-link :to="`/category?id=${v.id}`">{{ v.name }}</router-link>
      <div class="layer">
        <ul>
          <li v-for="(item, i) in v.children" :key="i">
            <a href="javaScript:;" @click="gOsub(item)">
              <img :src="item.picture" alt="" />
              <p>{{ item.name }}</p>
            </a>
          </li>
        </ul>
      </div>
    </li>
    <!-- <li><a href="#">餐厨</a></li>
    <li><a href="#">艺术</a></li>
    <li><a href="#">电器</a></li>
    <li><a href="#">居家</a></li>
    <li><a href="#">洗护</a></li>
    <li><a href="#">孕婴</a></li>
    <li><a href="#"></a></li>
    <li><a href="#">杂货</a></li> -->
  </ul>
</template>

<script>
import { inject, ref } from "vue";
import { getHeader, getClassify } from "@/api"; // 请求头部的数据
import { useRouter } from "vue-router";
import { useStore } from "vuex";
export default {
  name: "appHeaderNav",
  setup() {
     let store = useStore()
    let router = useRouter();
    let headerObj = ref([
      // 默认上来展示这些数据，防止白屏
      {
        name: "居家",
      },
      {
        name: "美食",
      },
      {
        name: "服饰",
      },
      {
        name: "母婴",
      },
      {
        name: "个护",
      },
      {
        name: "严选",
      },
      {
        name: "数码",
      },
      {
        name: "运动",
      },
      {
        name: "杂项",
      },
    ]);

    getHeader().then((res) => {
      headerObj.value = res.result;
    });
    // 跳转页面并传入id
    function goClassify(id) {
      // console.log(id);
      // router.push({ name: `/category`, params: { id: id } });
      // 如果使用 parmas router 配置里的name 和跳转的name 必须一样
      router.push({ path: `/category`, query: { id: id } });

      // :to="`/category/${v.id}?id=` + v.id"
    }

    function gOsub(obj) {
      getClassify().then((res) => {
        // 双重循环 循环子数据 找到父数据
        res.result.forEach((item) => {
          item.children.forEach((v) => {
            if (obj.id == v.id) {
              store.commit("bread", item);
            }
          });
        });
      });
      //   setTimeout(() => {
      //     localStorage.setItem("bread", JSON.stringify(obj.value));
      //   }, 1000);

      router.push({ path: "/category/sub", query: { id: obj.id } });
    }
    return {
      headerObj,
      goClassify,
      gOsub,
    };
  },
};
</script>

<style scoped lang="scss">
.app-header-nav {
  width: 820px;
  display: flex;
  padding-left: 40px;
  position: relative;
  z-index: 998;
  li {
    margin-right: 40px;
    width: 38px;
    text-align: center;
    a {
      font-size: 16px;
      line-height: 32px;
      height: 32px;
      display: inline-block;
    }
    &:hover {
      a {
      }
    }
  }
}
// 悬停出现2级菜单
.app-header-nav {
  width: 820px;
  display: flex;
  justify-content: space-around;
  padding-left: 40px;
  position: relative;
  z-index: 998;
  > li {
    margin-right: 40px;
    width: 38px;
    text-align: center;
    > a {
      font-size: 16px;
      line-height: 32px;
      height: 32px;
      display: inline-block;
    }
    &:hover {
      > a {
        color: $xtxColor;
        border-bottom: 1px solid $xtxColor;
      }
      > .layer {
        height: 132px;
        opacity: 1;
      }
    }
  }
}
.layer {
  width: 1240px;
  background-color: #fff;
  position: absolute;
  left: -200px;
  top: 56px;
  height: 0;
  overflow: hidden;
  opacity: 0;
  box-shadow: 0 0 5px #ccc;
  transition: all 0.2s 0.1s;
  ul {
    display: flex;
    flex-wrap: wrap;
    padding: 0 70px;
    align-items: center;
    height: 132px;
    li {
      width: 110px;
      text-align: center;
      img {
        width: 60px;
        height: 100%;
        flex-shrink: 0;
      }
      p {
        padding-top: 10px;
      }
      &:hover {
        p {
          color: $xtxColor;
        }
      }
    }
  }
}
</style>
